using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace derp
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

        SpriteBatch spriteBatch;

        Texture2D ballsprite;

        SoundEffect effect1;
        SoundEffect effect2;

        // Use MediaPlayer.Play(song1); to play the song
        Song song1; //for background music.
        Song song2; //testing simultaneous play

        //XACT stuff
        /*
        AudioEngine audioEngine;
        WaveBank waveBank;
        SoundBank soundBank;

        Cue engineSound = null;
        */

        //Now with My datastructures
        classes.soundEngine sEngine;
        classes.waveBank    wBank;
        classes.soundBank   sBank;
        classes.cue         cue; //Perhaps a list of these

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            ballsprite = Content.Load<Texture2D>("images");

            effect1 = Content.Load<SoundEffect>("audio/missilelaunch");
            effect2 = Content.Load<SoundEffect>("audio/hyperspace_activate");

            song1 = Content.Load<Song>("audio/02. Dance Or Die (feat. Saul Williams)");
            song2 = Content.Load<Song>("audio/03. Faster");

            //Xact
            /*
            audioEngine = new AudioEngine("Content\\audio\\GameAudio.xgs");
            waveBank = new WaveBank(audioEngine, "Content\\audio\\Wave Bank.xwb");
            soundBank = new SoundBank(audioEngine, "Content\\audio\\Sound Bank.xsb");
            */

            //Class based
            sEngine = new derp.classes.soundEngine("Content\\audio\\GameAudio.xgs");
            wBank = new derp.classes.waveBank("Content\\audio\\Wave Bank.xwb"
                                                , sEngine.getEngine());
            sBank = new derp.classes.soundBank("Content\\audio\\Sound Bank.xsb"
                                                , sEngine.getEngine());
            cue = new derp.classes.cue("engine_2", sBank.getSoundBank());

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.A))
            {
                //Move Left;
            }
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.D))
            {
                //Move Right;
            }
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.W))
            {
                //Jump;
                
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up))
            {
                //Play current Tune;
                    //effect1.Play(0.5f, 0.0f, 0.0f);
                    //effect2.Play(0.5f, 0.0f, 0.0f);
                if (cue.getCue() == null)
                {
                    //engineSound = soundBank.GetCue("engine_2");
                    //engineSound.Play();
                    cue.setSound("engine_2", sBank.getSoundBank());
                    cue.playSound();
                }
                else if (cue.getCue().IsPaused)
                {
                    //engineSound.Resume();
                    cue.resumeSound();
                }
                else if (!cue.getCue().IsPlaying)
                {
                    cue.playSound();
                }
                //first is volume
                //second is pitch
                //third is pan
            }
            if (Keyboard.GetState(PlayerIndex.One).IsKeyUp(Keys.Up))
            {
                if (cue.getCue() != null && cue.getCue().IsPlaying)
                {
                    //engineSound.Pause();
                    cue.pauseSound();
                }
            }
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left))
            {
                //Go one left in the tunelist;
                MediaPlayer.Play(song1);
                MediaPlayer.Play(song2);
                //Its one song or the other, not mixed
            }
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right))
            {
                //Go one right in the tunelist;
                
                MediaPlayer.Stop(); //stop the background music
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down))
            {
                //soundBank.PlayCue("hyperspace_activate");
                sBank.getSoundBank().PlayCue("hyperspace_activate");
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            //Window.ClientBounds.Width;
            //Window.ClientBounds.Height;

            spriteBatch.Begin();
            spriteBatch.Draw(ballsprite, new Rectangle(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2, 80, 80), Color.Beige);
            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
